#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/11/14 10:58
# @Author  : lizhen
# @Site    : 
# @File    : cReplyMsg.py
# @Software: PyCharm
import hashlib
import time

import xmltodict


class ReplyMsgDemo:
    recv_simple_msg_type_list = ['text', 'image', 'voice', 'video', 'shortvideo', 'location', 'link']
    recv_event_msg_type_list = ['event']
    recv_event_type_list = ['subscribe', 'unsubscribe', 'SCAN', 'LOCATION', 'CLICK', 'VIEW', 'scancode_push',
                            'scancode_waitmsg', 'location_select', 'pic_sysphoto', 'pic_photo_or_album', 'pic_weixin']
    send_msg_type_list = ['text', 'image', 'voice', 'video', 'music', 'news']

    def check_status(self, request):
        signature = request.args.get('signature')
        timestamp = request.args.get('timestamp')
        nonce = request.args.get('nonce')
        echostr = request.args.get('echostr')
        token = "python"
        data = [token, timestamp, nonce]
        print data
        data.sort()
        data = ''.join(data)
        sha1 = hashlib.sha1()
        sha1.update(data)
        hashcode = sha1.hexdigest()
        print "handle/GET func: hashcode, signature: ", hashcode, signature
        if hashcode == signature:
            return echostr, 200
        else:
            return "error", 666

    def reply_msg(self, request):
        print request

        _from = request.get('FromUserName')
        _to = request.get('ToUserName')
        _create_time = int(time.time())
        _msg_type = request.get('MsgType')

        _r_code = request.get('r_code')  # 响应码

        _content = request.get('Content', '')  # 文本内容

        _pic_url = request.get('PicUrl', '')  # 图片地址

        _media_id = request.get('MediaId', '')  # 文件id
        _format = request.get('Format', '')  # 文件格式
        _recognition = '无法识别' if request.get('Recognition', u'未打开语音识别功能') is None else request.get(
            'Recognition')  # 语音识别

        _thumb_media_id = request.get('ThumbMediaId', '')  # 视频id

        _location_x = request.get('Location_X', '')  # 地理位置维度
        _location_y = request.get('Location_Y', '')  # 地理位置经度
        _scale = request.get('Scale', '')  # 地图缩放大小
        _label = request.get('Label', '')  # 地理位置信息

        _title = request.get('Title', '')  # 消息标题
        _description = request.get('Description', '')  # 消息描述
        _url = request.get('Url', '')  # 消息链接

        _event = request.get('Event')  # 事件

        _event_key = '' if request.get('EventKey', '') is None else request.get('EventKey')  # 事件键
        _ticket = request.get('Ticket', '')  # 二维码票据

        _latitude = request.get('Latitude', '')  # 地理位置纬度
        _longitude = request.get('Longitude', '')  # 地理位置经度
        _precision = request.get('Precision', '')  # 地理位置精度

        _scan_code_info = request.get('ScanCodeInfo', '')  # 扫描信息
        _scan_type = request.get('ScanType', '')  # 扫描类型，一般是qrcode
        _scan_result = request.get('ScanResult', '')  # 扫描结果，即二维码对应的字符串信息

        _send_location_info = request.get('SendLocationInfo', '')  # 发送的位置信息
        _poi_name = request.get('Poiname', '')  # 朋友圈POI的名字

        _send_pics_info = request.get('SendPicsInfo', '')  # 发送的图片信息
        _count = request.get('Count', '')  # 发送的图片数量
        _pic_list = request.get('PicList', '')  # 图片列表
        _pic_md5_sum = request.get('PicMd5Sum', '')  # 图片的MD5值

        if _msg_type == 'text':
            resp = {
                'ToUserName': _from,
                'FromUserName': _to,
                'CreateTime': _create_time,
                'MsgType': _msg_type,
                'Content': _content
            }
        elif _msg_type == 'image':
            resp = {
                'ToUserName': _from,
                'FromUserName': _to,
                'CreateTime': _create_time,
                'MsgType': 'text',
                'Content': 'PicUrl: {0}, MediaId: {1}'.format(_pic_url, _media_id)
            }
        elif _msg_type == 'voice':
            resp = {
                'ToUserName': _from,
                'FromUserName': _to,
                'CreateTime': _create_time,
                'MsgType': 'text',
                'Content': 'MediaId: {0}, Format: {1}, Recognition: {2}'.format(_media_id, _format, _recognition)
            }
        elif _msg_type == 'video':
            resp = {
                'ToUserName': _from,
                'FromUserName': _to,
                'CreateTime': _create_time,
                'MsgType': 'text',
                'Content': 'MsgType: {0}, MediaId: {1}, ThumbMedisId: {2}'.format(_msg_type, _media_id, _thumb_media_id)
            }
        elif _msg_type == 'shortvideo':
            resp = {
                'ToUserName': _from,
                'FromUserName': _to,
                'CreateTime': _create_time,
                'MsgType': 'text',
                'Content': 'MsgType: {0}, MediaId: {1}, ThumbMedisId: {2}'.format(_msg_type, _media_id, _thumb_media_id)
            }
        elif _msg_type == 'location':
            resp = {
                'ToUserName': _from,
                'FromUserName': _to,
                'CreateTime': _create_time,
                'MsgType': 'text',
                'Content': 'Location_X: {0}, Location_Y: {1}, Scale: {2}, Label: {3}'.format(_location_x, _location_y,
                                                                                             _scale, _label)
            }
        elif _msg_type == 'link':
            resp = {
                'ToUserName': _from,
                'FromUserName': _to,
                'CreateTime': _create_time,
                'MsgType': 'text',
                'Content': 'Title: {0}, Description: {1}, Url: {2}'.format(_title, _description, _url)
            }
        elif _msg_type == 'event':
            # 'subscribe', 'unsubscribe', 'SCAN', 'LOCATION', 'CLICK', 'VIEW'
            if _event == 'subscribe' and _event_key == '':
                resp = {
                    'ToUserName': _from,
                    'FromUserName': _to,
                    'CreateTime': _create_time,
                    'MsgType': 'text',
                    'Content': 'Event: {}'.format(_event)
                }
            elif _event == 'subscribe' and _event_key != '':
                resp = {
                    'ToUserName': _from,
                    'FromUserName': _to,
                    'CreateTime': _create_time,
                    'MsgType': 'text',
                    'Content': 'Event: {0}, EventKey: {1}, Ticket: {2}'.format(_event, _event_key, _ticket)
                }
            elif _event == 'unsubscribe' and _event_key == '':
                resp = {
                    'ToUserName': _from,
                    'FromUserName': _to,
                    'CreateTime': _create_time,
                    'MsgType': 'text',
                    'Content': 'Event: {}'.format(_event)
                }
            elif _event == 'unsubscribe' and _event_key != '':
                resp = {
                    'ToUserName': _from,
                    'FromUserName': _to,
                    'CreateTime': _create_time,
                    'MsgType': 'text',
                    'Content': 'Event: {0}, EventKey: {1}, Ticket: {2}'.format(_event, _event_key, _ticket)
                }
            elif _event == 'LOCATION':
                resp = {
                    'ToUserName': _from,
                    'FromUserName': _to,
                    'CreateTime': _create_time,
                    'MsgType': 'text',
                    'Content': 'Event: {0}, Latitude: {1}, Longtitude: {2}, Precision: {3}'.format(_event, _latitude,
                                                                                                   _longitude,
                                                                                                   _precision)
                }
            elif _event == 'CLICK':
                resp = {
                    'ToUserName': _from,
                    'FromUserName': _to,
                    'CreateTime': _create_time,
                    'MsgType': 'text',
                    'Content': 'Event: {0}, EventKey: {1}'.format(_event, _event_key)
                }
            elif _event == 'VIEW':
                resp = {
                    'ToUserName': _from,
                    'FromUserName': _to,
                    'CreateTime': _create_time,
                    'MsgType': 'text',
                    'Content': 'Event: {0}, EventKey: {1}'.format(_event, _event_key)
                }
            elif _event in ['scancode_push', 'scancode_waitmsg']:
                resp = {
                    'ToUserName': _from,
                    'FromUserName': _to,
                    'CreateTime': _create_time,
                    'MsgType': 'text',
                    'Content': 'Event: {0}, EventKey: {1}, ScanCodeInfo: {2}, ScanType: {3}, ScanResult: '
                               '{4}'.format(_event, _event_key, _scan_code_info, _scan_type, _scan_result)
                }
            elif _event == 'location_select':
                resp = {
                    'ToUserName': _from,
                    'FromUserName': _to,
                    'CreateTime': _create_time,
                    'MsgType': 'text',
                    'Content': 'Event: {0}, EventKey: {1}, SendLocationInfo: {2}, Location_X: {3}, Location_Y: '
                               '{4}, Scale: {5}, Label: {6}, Poiname: {7}'.format(_event, _event_key,
                                                                                  _send_location_info, _location_x,
                                                                                  _location_y, _scale, _label,
                                                                                  _poi_name)
                }
            elif _event in ['pic_sysphoto', 'pic_photo_or_album', 'pic_photo_or_album']:
                resp = {
                    'ToUserName': _from,
                    'FromUserName': _to,
                    'CreateTime': _create_time,
                    'MsgType': 'text',
                    'Content': 'Event: {0}, EventKey: {1}, SendPicsInfo: {2}, Count: {3}, PicList: '
                               '{4}, PicMd5Sum: {5}'.format(_event, _event_key, _send_pics_info, _count,
                                                            _pic_list, _pic_md5_sum)
                }
        elif _msg_type not in self.recv_simple_msg_type_list and _msg_type not in self.recv_event_msg_type_list:
            _content = '请求格式不支持'
            _msg_type = 'text'
            _r_code = 666
            resp = {
                'ToUserName': _from,
                'FromUserName': _to,
                'CreateTime': _create_time,
                'MsgType': _msg_type,
                'Content': _content
            }
        print resp
        xml = xmltodict.unparse({'xml': resp})
        return xml, _r_code


if __name__ == '__main__':
    pass
